package logic

import (
	"context"
	"errors"
	"short-video-dev/mysql"

	"short-video-dev/videorpc/internal/svc"
	"short-video-dev/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UploadVideoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUploadVideoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadVideoLogic {
	return &UploadVideoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 视频上传
func (l *UploadVideoLogic) UploadVideo(in *videorpc.UploadVideoRequest) (*videorpc.UploadVideoResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.UserInfo
	err := user.FindUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("用户不存在,请先注册")
	}

	var video = &mysql.VideoInfo{
		UserId:        int(in.UserId),
		VideoTitle:    in.VideoTitle,
		VideoCategory: in.VideoCategory,
		VideoLink:     in.VideoLink,
		CoverLink:     in.CoverLink,
		VideoIntro:    in.VideoIntro,
		VideoStatus:   int(in.VideoStatus),
		TopicId:       int(in.TopicId),
	}

	err = video.VideoAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("视频上传失败")
	}

	return &videorpc.UploadVideoResponse{
		VideoId: int64(video.ID),
	}, nil
}
